
%
%
%
function [differentiated, preprocessed] = qt_detector_filter(x, fs)

    [size_x,size_y] =size(x);
    
    if(size_y ~= 1)
        error('Input vector should have Nx1 dimentions');
    end

    preFilter = preprocess_filter(fs);
        
    preFilterDelay = floor(preFilter.order/2);
    

    prefiltered = circshift(filter(preFilter, x),[-preFilterDelay 0]);
    
%     prefiltered(1:24) = 0;


    hpZerosOrder = floor(6 * fs /200)+1;
    hpFilterDelay = floor(hpZerosOrder/2);
   
    num1 = zeros(1,hpZerosOrder);
    num1(1) = 1;
    num1(hpZerosOrder) = -1;
    den1 = zeros(1,hpZerosOrder);
    den1(1) = 1;
    
    y1 =  circshift(filter(num1,den1,prefiltered), [-hpFilterDelay 0]); 

    y1(1:24) = 0;
    y1(length(y1)-24:length(y1)) = 0;
    
    differentiated = y1 /max(y1);
    
    Hlp = qt_lp_filter(fs);
    lpFilterDelay = floor(Hlp.order/2);
    
    preprocessed = circshift(filter(Hlp, differentiated),[-lpFilterDelay 0]);
    
    y2(1:24) = 0;
    
    senAux = (prefiltered - mean(prefiltered))/max(prefiltered);
    senAux2 = (x - mean(x))/max(x);
    senAux3 = differentiated /max(differentiated);
    senAux4 = preprocessed /max(preprocessed);
    
%     figure(1);
%     hold;
%     signalPlot = plot(senAux,'b');
%     signalPlot = plot(senAux2,'r');
%     signalPlot = plot(senAux3,'g');
%     signalPlot = plot(senAux4,'c');


    
%     G3 = G2*G1
% 
%     [num3Cell,den3Cell] = tfdata(G3);
%     num3 = num3Cell{1,:};
%     den3 = den3Cell{1,:};

%     out = filter(num3,den3,x); 

%     figure;
%     pzmap(G1)
%     zgrid
%     %axis('square')
%     figure;
%     freqz(num1,den1,100000,fs);
%      
%     figure;
%     pzmap(G2)
%     zgrid
%     %axis('square')
%     figure;
%     freqz(num2,den2,100000,fs);
    
    % figure;
    % pzmap(G3);
    % zgrid
    % figure;
    % freqz(num3,den3,10000,fs);
end


function Hd = qt_lp_filter(Fs)
%FILTER Returns a discrete-time filter object.

%
% M-File generated by MATLAB(R) 7.10 and the Signal Processing Toolbox 6.13.
%
% Generated on: 05-Feb-2012 20:02:37
%

Fpass = 15;   % Passband Frequency 15
Fstop = 20;   % Stopband Frequency 20
Apass = 1;    % Passband Ripple (dB)
Astop = 3;    % Stopband Attenuation (dB)
%Fs    = 360;  % Sampling Frequency

h = fdesign.lowpass('fp,fst,ap,ast', Fpass, Fstop, Apass, Astop, Fs);

Hd = design(h, 'equiripple', ...
    'MinOrder', 'even', ...
    'StopbandShape', 'flat');


end

function Hd = preprocess_filter(Fs)
%UNTITLED Returns a discrete-time filter object.

%
% M-File generated by MATLAB(R) 7.10 and the Signal Processing Toolbox 6.13.
%
% Generated on: 05-Mar-2012 20:20:20
%

N    = 20;   % Order
F6dB = 60;  %
%Fs   = 360;  % Sampling Frequency

h = fdesign.lowpass('n,fc', N, F6dB, Fs);

Hd = design(h, 'window');
end

% [EOF]
